"use strict";

/**
 * 用户相关接口调用
 */
import Ajax from "../libs/ajax";
import Constant from "../libs/constants";

export function addOrUpdateStaff(url, data) {
    return Ajax.ajax({
        url: "/api/user/" + url,
        data: data,
        method: "post"
    }).then(res => res.data);
}

export function updateAvatar(data) {
    return Ajax.ajax({
        url: "/api/user/update_avatar",
        data: data,
        method: "post"
    }).then(res => res.data)
}

export function deleteUser(data) {
    return Ajax.ajax({
        url: "/api/user/delete",
        data: data,
        method: "post"
    }).then(res => res.data);
}

export function getStaffData(page_num, page_size, user_name) {
    const baseUrl = "/api/user/query?";
    if (page_num === undefined || page_size === undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "员工查询参数缺失" });
        });
    }
    /*to-do:参数校验
    //除了required的键，其余参数容许缺失。
    //required:page_num存在->为数字->上下限
    if(page_num===undefined){
      console.log("页面缺失,默认值：1");
    }
    //required:page_size存在->为数字->上下限
    if(page_size===undefined){
      console.log("页面缺失，默认值：3");
    }
    //user_name存在->长度限制->恶意编码
    if(user_name===undefined){
      console.log("返回全页");
    }
    */
    let queryArg = [
        `page_num=${page_num}`,
        `page_size=${page_size}`
    ];

    if (user_name !== undefined) {
        queryArg.push(`user_name=${user_name}`);
    }

    return Ajax.ajax({
        url: baseUrl + queryArg.join("&"),
        method: "get",
        timeout: 20 * 1000,
    }).then(res => res.data);
}

export function getDepartmentList(type) {
    return Ajax.ajax({
        url: `/api/config/query?type=${type}`,
        method: "get"
    }).then(res => res.data);
}

export function getAvatar(user_id) {
    return Ajax.ajax({
        url: `/api/user/query_avatar?user_id=${user_id}`,
        timeout: 1000000,
        method: "get"
    }).then(res => res.data);
}

export function getFeature(userId) {
    return Ajax.ajax({
        url: `/api/user/query_feature?user_id=${userId}`,
        method: "get",
        timeout: 0,
    }).then(res => res.data);
}

export function regFeature(data) {
    return Ajax.ajax({
        url: "/api/user/register_feature",
        data: data,
        method: "post"
    }).then(res => res.data);
}

export function addFeature(data) {
    return Ajax.ajax({
        url: "/api/user/add_feature",
        data: data,
        method: "post"
    }).then((res) => res.data);
}

export function delFeature(data) {
    return Ajax.ajax({
        url: "/api/user/delete_feature",
        data: data,
        method: "post"
    }).then(res => res.data);
}

export function addOrUpdateGuest(url, data) {
    return Ajax.ajax({
        url: "/api/user/" + url,
        method: "post",
        data: data
    }).then(res => res.data);
}

export function getStrangerData(page_num, page_size, user_name) {
    const baseUrl = "/api/user/query?";
    if (page_num === undefined || page_size === undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "陌生人查询参数缺失" });
        });
    }
    let queryArg = [
        `page_num=${page_num}`,
        `page_size=${page_size}`
    ];

    if (user_name !== undefined) {
        queryArg.push(`user_name=${user_name}`);
    }

    queryArg.push('type=stranger');

    return Ajax.ajax({
        url: baseUrl + queryArg.join("&"),
        method: "get"
    }).then(res => res.data);
}

export function getVipData(page_num, page_size, user_name) {
    const baseUrl = "/api/user/query?";
    if (page_num === undefined || page_size === undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "vip 查询参数缺失" });
        });
    }
    let queryArg = [
        `page_num=${page_num}`,
        `page_size=${page_size}`
    ];

    if (user_name !== undefined) {
        queryArg.push(`user_name=${user_name}`);
    }

    queryArg.push('type=vip');

    return Ajax.ajax({
        url: baseUrl + queryArg.join("&"),
        method: "get"
    }).then(res => res.data);
}

export function getGuestData(page_num, page_size, user_name) {
    const baseUrl = "/api/user/query?";
    if (page_num === undefined || page_size === undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "访客查询参数缺失" });
        });
    }
    let queryArg = [
        `page_num=${page_num}`,
        `page_size=${page_size}`
    ];

    if (user_name !== undefined) {
        queryArg.push(`user_name=${user_name}`);
    }

    queryArg.push('type=guest')

    return Ajax.ajax({
        url: baseUrl + queryArg.join("&"),
        method: "get"
    }).then(res => res.data);
}

//员工批量上传
export function uploadStaffData(data) {
    return Ajax.ajax({
        url: Constant.URL_UPLOAD_STAFF,
        method: 'post',
        data: data
    }).then(res => res.data)
}

//访客批量上传
export function uploadGuestData(data) {
    return Ajax.ajax({
        url: Constant.URL_UPLOAD_GUEST,
        method: 'post',
        data: data
    }).then(res => res.data)
}

export function registBatch(data) {
    return Ajax.ajax({
        url: "",
        data: data
    }).then(res => res.data)
}

export function registFeatureBatch(data) {
    return Ajax.ajax({
        url: Constant.URL_UPLOAD_FEATURE_BATCH,
        data: data,
        timeout: 60 * 60 * 1000,
        method: 'post'
    }).then(res => res.data)
}

export function mergeFeature(from, to) {
    if (from === undefined || to === undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "员工合并特征参数缺失" });
        });
    }
    return Ajax.ajax({
        url: Constant.URL_USER_MERGE_FEATURE,
        data: {
            from_id: from,
            to_id: to,
        },
        method: 'post',
    }).then(res => res.data)
}

export function recovery(user_id) {
    if (user_id == undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "员工合并特征参数缺失" });
        });
    }
    return Ajax.ajax({
        url: Constant.URL_USER_RECOVERY,
        data: {
            user_id: user_id,
        },
        method: 'post',
    }).then(res => res.data)
}

export function cleanUserType(user_type) {
    return Ajax.ajax({
        url: Constant.URL_USER_CLEAN,
        data: {
            types: [user_type]
        },
        method: 'post',
    }).then(res => res.data);
}

export function emptyStudentFeature() {
    return Ajax.ajax({
        url: '/api/user/empty_student_feature',
        method: 'post',
    }).then(res => res.data);
}

export function syncStudentFeature() {
    return Ajax.ajax({
        url: '/api/user/sync_student_feature',
        method: 'post',
    }).then(res => res.data);
}